package com.cyc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyc.mapper.InformationMapper;
import com.cyc.model.Information;
import com.cyc.tools.Layui;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InfomationService {
    @Resource
    private InformationMapper informationMapper;

    public Layui search(Information information, int page, int size) {
        QueryWrapper<Information> wrapper = new QueryWrapper<>();
        if(!"".equals(information.getName())){
            wrapper.eq("Sname",information.getName());
        }
        if(!"".equals(information.getDept())){
            wrapper.eq("dept",information.getDept());
        }
        if(information.getAge()!=null){
            wrapper.eq("age",information.getAge());
        }
        if(!"".equals(information.getSex())){
            wrapper.eq("sex",information.getSex());
        }
        List<Information> list = informationMapper.selectList(wrapper);
        return Layui.data(list,200,list.size());
    }

    public int addOrEditOne(Information information) {
        int i;
        if(information.getId()==null){
            //添加
            i=informationMapper.insert(information);
        }else{
            i=informationMapper.updateById(information);
        }
        return i;
    }

    public Layui getAll(Integer page, Integer size) {
        //参数一是当前页，参数二是每页个数
        Page<Information> page1 = new Page<>(page, size);
        informationMapper.selectPage(page1,null);
        List<Information> results = page1.getRecords();
        return Layui.data(results,200,results.size());
    }

    public int delOne(Information information) {
        int i=0;
        if(information.getId()!=null){
            i = informationMapper.deleteById(information.getId());
        }
        return i;
    }
    //用户登录后获取自己信息
    public Layui getOne(String username) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",username);
        List<Information> list = informationMapper.selectList(wrapper);
        return Layui.data(list,200,1);
    }
}
